using System;
using System.Collections.Generic;
using System.Text;

namespace mds.Queue
{
	/// <summary>
	/// This class is basically used to keep tabs on all QueueProcessors created
	/// in a particular application.  It's primarily used for generating reports
	/// for all queues.  These are externally accessable through Beisen.Queue.Manager.QueueManager.
	/// </summary>
	internal static class ReportableObjectDirectory
	{
		private static Dictionary<string, IReportable> reportableObjects = new Dictionary<string, IReportable>();

		/// <summary>
		/// Adds a reportable object to the list
		/// </summary>
		/// <param name="obj">an IReportable object</param>
		public static void Add(string name, IReportable obj)
		{
			//Console.WriteLine("Adding " + name);
			lock (reportableObjects)
			{
				if (reportableObjects.ContainsKey(name))
				{
					reportableObjects[name] = obj;
				}
				else
				{
					reportableObjects.Add(name, obj);
				}
			}
		}

		/// <summary>
		/// Remove a reportable object from the list
		/// </summary>
		/// <param name="obj">an IReportable object you want removed</param>
		public static void Remove(string name)
		{
			Console.WriteLine("Removing " + name);
			lock (reportableObjects)
			{
				reportableObjects.Remove(name);
			}
		}

		/// <summary>
		/// Returns a report of all the reportable objects stored here
		/// </summary>
		/// <returns></returns>
		public static string CreateReport()
		{
			StringBuilder sb = new StringBuilder();
			lock (reportableObjects)
			{
				foreach (IReportable reportable in reportableObjects.Values)
				{
					sb.Append(reportable.CreateReport());
				}
			}

			return sb.ToString();
		}

		/// <summary>
		/// Returns the number of reportable objects
		/// </summary>
		public static int ReportableCount
		{
			get { return reportableObjects.Count; }
		}
	}
}
